$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    //console.log(url);

    $(".icon-left").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    $(".head h1").text(localStorage.getItem("user_name") + "复秤入库记录");

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "product_lishi.html";
    });

    $("#goods_id").focus();

    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            var val = $(this).val();
            // $("#goods_id").blur();
            if (!val) {
                $alert("请扫描产品标签！");
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $("#goods_id").val("");
            //加载提示符 显示
            loading_show("搜索中...");
            getIndata(val)
        }
    });

    //获取本人包装入库记录
    getIndata();

    function getIndata(batch) {
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productinorder/getProductwarehousesByUser',
            data: {
                batch: batch
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                loading_hide();
                if (res.success) {
                    var data = res.result;
                    var allWeight = 0;
                    if (!data || data.length == 0) {
                        $alert("暂无入库记录！");
                    } else {
                        $("#ul_data").empty();
                        var data_html = '';
                        for (var i = 0; i < data.length; i++) {
                            data_html += '<li><div class="row no-gutter"><div class="col-90">' +
                                '<p><span>编号：</span><span class="number sp">' + (data[i].batch ? data[i].batch : "空") + '</span></p>' +
                                '<p><span>排产单：</span><span class="workplanNumber sp">' + (data[i].workplanNumber || "空") + '</span></p>' +
                                '<p><span>客户：</span><span class="customer sp">' + (data[i].customerName || "空") + '</span></p>' +
                                '<p><span>存货名称：</span><span class="inventoryName sp">' + (data[i].inventoryName ? data[i].inventoryName : "") + '</span></p>' +
                                '<p><span>存货规格：</span><span class="specification sp">' + (data[i].inventorySpecification ? data[i].inventorySpecification : "") + '</span></p>' +
                                '<p><span>净重：</span><span class="weight sp">' + (data[i].quantity ? data[i].quantity : data[i].quantity) + '</span>' +
                                '<span>T 人员：</span><span class="user sp">' + data[i].inName + '</span></p>' +
                                '<p><span>时间：</span><span class="in_time sp">' + data[i].inTime + '</span></p>' +
                                '<p><span>仓库：</span><span class="houseName sp">' + data[i].warehouseName + '</span></p>' +
                                '</div><div class="col-10"><span class="icon iconfont icon-dayin pull-right"></span></div>' +
                                '</div></li>';
                            allWeight = accAdd(allWeight, data[i].quantity);
                        }
                        $("#ul_data").append(data_html);
                    }
                    //改变 总计数
                    $(".content div.ul_foot span.sp_js").html($("#ul_data li").length);
                    $(".content div.ul_foot span.sp_weight").html(allWeight);
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    }

    //点击 打印
    $("#ul_data").on("click", ".col-10", function () {
        var number = $(this).parent().parent().find("span.number").text();
        var weight = $(this).parent().parent().find("span.weight").text();
        loading_show("打印中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productinorder/printProductarehouse',
            data: {
                batch: number
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    $alert(number + "打印成功");
                    if (/一期|二期/.test(data.warehouseName)) {
                        //打印钢丝成品标签
                        takeupPrint("wireProduct", data);
                    } else {
                        //打印钢绞线成品标签
                        takeupPrint("steelStrandProduct", data);
                    }
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                console.log(error);
                loading_hide();
                $alert(error.responseText);
            }
        });
    })

});
